<?php
namespace app\api\controller;

use app\common\controller\BaseController;
use app\common\repositories\users\UsersRepository;
use support\Container;
use support\Request;

/**
 * Users
 */
class UsersController extends BaseController
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new UsersRepository();
    }

    /**
     * @desc: create one Users
     * @param varchar   $nickname      登录用户名
     * @param varchar   $password      密码哈希值
     * @param varchar   $password_confirm      用户显示昵称
     * @param varchar   $avatar        头像图片URL
     * @param varchar   $verify_code        头像图片UR
     * @param varchar   $invite_id        头像图片URL
     * @author: generate
     */
    public function register()
    {
        $email = request()->post('email',null);
        $password = request()->post('password',null);
        $result = $this->repository->registerOneUsers(
            $email,
            $password
        );
        return json($result);
    }
    /**
     * @desc: login one user
     * @author: generate
     */
    public function login(){
        $email = request()->post('email',null);
        $password = request()->post('password',null);
        $result = $this->repository->loginOneUsers(
            $email,
            $password
        );
        return json($result);
    }
    /**
     * @desc: get one Users detail
     * @author: generate
     */
    public function info(){
        $user_id = request()->user->id ?? 0;
        $data = $this->repository->getDetail($user_id);
        $result = ['code' => 200, 'data' => $data];
        return json($result);
    }

    /**
     * @desc: get one Users detail
     * @author: generate
     */
    public function avatar(){
        $user_id = request()->user->id ?? 0;
        $avatar = request()->post('avatar',null);
        $result = $this->repository->avatar($user_id,$avatar);
        return json($result);
    }

    /**
     * @desc: user login out
     * @author: xiaohp
     */
    public function loginout(){
        $user_id = request()->user->id ?? 0;
        $result = $this->repository->loginout($user_id);
        return json($result);
    }

}